home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / layout / example / appletbutton.000 next >
Encoding:
Text File  |  1996-02-26  |  5.4 KB  |  194 lines

  1. <!--NewPage-->
  2. <html>
  3. <head>
  4. <title>AppletButton.java</title>
  5. </head>
  6. <body>
  7. <p>
  8. <hr size=4>
  9.  
  10. <h2>
  11.     AppletButton.java
  12. </h2>
  13. <p>
  14. <blockquote>
  15.  
  16. <pre>
  17.  
  18. /*
  19.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software
  22.  * and its documentation for NON-COMMERCIAL purposes and without
  23.  * fee is hereby granted provided that this copyright notice
  24.  * appears in all copies. Please refer to the file "copyright.html"
  25.  * for further important copyright and licensing information.
  26.  *
  27.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  28.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  29.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  30.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  31.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  32.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  33.  */
  34. //TO DO: Close all windows when you leave the page?
  35.  
  36. import java.awt.*;
  37. import java.util.*;
  38. import java.applet.Applet;
  39.  
  40. public class AppletButton extends Applet implements Runnable {
  41.     int frameNumber = 1;
  42.     String windowClass;
  43.     String buttonText;
  44.     String windowText;
  45.     int requestedWidth = 0;
  46.     int requestedHeight = 0;
  47.     Button button;
  48.     Thread windowThread;
  49.     Label label;
  50.     boolean pleaseCreate = false;
  51.  
  52.     public void init() {
  53.     windowClass = getParameter("WINDOWTYPE");
  54.     if (windowClass == null) {
  55.         windowClass = "TestWindow";
  56.     }
  57.  
  58.     buttonText = getParameter("BUTTONTEXT");
  59.     if (buttonText == null) {
  60.         buttonText = "Click here to bring up a " + windowClass;
  61.     }
  62.  
  63.     windowText = getParameter("WINDOWTEXT");
  64.     if (windowText == null) {
  65.         windowText = windowClass;
  66.     }
  67.  
  68.         String windowWidthString = getParameter("WINDOWWIDTH");
  69.         if (windowWidthString != null) {
  70.             try {
  71.                 requestedWidth = Integer.parseInt(windowWidthString);
  72.             } catch (NumberFormatException e) {
  73.                 requestedWidth = 0;
  74.             }
  75.         }
  76.  
  77.         String windowHeightString = getParameter("WINDOWHEIGHT");
  78.         if (windowHeightString != null) {
  79.             try {
  80.                 requestedHeight = Integer.parseInt(windowHeightString);
  81.             } catch (NumberFormatException e) {
  82.                 requestedHeight = 0;
  83.             }
  84.         }
  85.  
  86.     setLayout(new GridLayout(2,0));
  87.     add(button = new Button(buttonText));
  88.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  89.  
  90.     add(label = new Label("", Label.CENTER));
  91.     }
  92.  
  93.     public void start() {
  94.     if (windowThread == null) {
  95.         windowThread = new Thread(this, "Bringing Up " + windowClass);
  96.         windowThread.start();
  97.     }
  98.     }
  99.  
  100.     public synchronized void run() {
  101.     Class windowClassObject = null;
  102.     Class tmp = null;
  103.     String name = null;
  104.     
  105.     // Make sure the window class exists and is really a Frame.
  106.     // This has the added benefit of pre-loading the class,
  107.     // which makes it much quicker for the first window to come up.
  108.     try {
  109.         windowClassObject = Class.forName(windowClass);
  110.     } catch (Exception e) {
  111.         // The specified class isn't anywhere that we can find.
  112.         label.setText("Can't create window: Couldn't find class "
  113.                   + windowClass);
  114.         button.disable();
  115.     }
  116.  
  117.     // Find out whether the class is a Frame.
  118.     for (tmp = windowClassObject, name = tmp.getName();
  119.          !( name.equals("java.lang.Object") ||
  120.             name.equals("java.awt.Frame") ); ) {
  121.         tmp = tmp.getSuperclass();
  122.         name = tmp.getName();
  123.     }
  124.     if ((name == null) || name.equals("java.lang.Object")) {
  125.         //We can't run; ERROR; print status, never bring up window
  126.         label.setText("Can't create window: "
  127.                   + windowClass +
  128.               " isn't a Frame subclass.");
  129.         button.disable();
  130.     } else if (name.equals("java.awt.Frame")) { 
  131.         //Everything's OK. Wait until we're asked to create a window.
  132.         while (windowThread != null) {
  133.             while (pleaseCreate == false) {
  134.             try {
  135.                 wait();
  136.             } catch (InterruptedException e) {
  137.             }
  138.             }
  139.  
  140.             //We've been asked to bring up a window.
  141.             pleaseCreate = false;
  142.             Frame window = null;
  143.             try {
  144.                 window = (Frame)windowClassObject.newInstance();
  145.             } catch (Exception e) {
  146.             label.setText("Couldn't create instance of class "
  147.                       + windowClass);
  148.             }
  149.         if (frameNumber == 1) {
  150.                 window.setTitle(windowText);
  151.         } else {
  152.                 window.setTitle(windowText + ": " + frameNumber);
  153.         }
  154.             frameNumber++;
  155.  
  156.         //Set the window's size.
  157.             window.pack();
  158.                 if ((requestedWidth > 0) | (requestedHeight > 0)) {
  159.                     window.resize(Math.max(requestedWidth,
  160.                        window.size().width),
  161.                   Math.max(requestedHeight,
  162.                        window.size().height));
  163.                 }
  164.  
  165.             window.show();
  166.             label.setText("");
  167.         }
  168.     }
  169.     }
  170.         
  171.     public synchronized boolean action(Event event, Object what) {
  172.     if (event.target instanceof Button) {
  173.         //signal the window thread to build a window
  174.         label.setText("Please wait while the window comes up...");
  175.         pleaseCreate = true;
  176.         notify();
  177.     } 
  178.     return false;
  179.     }
  180. }
  181.  
  182. class TestWindow extends Frame {
  183.     public TestWindow() {
  184.     resize(300, 300);
  185.     }
  186. }
  187. </pre>
  188. </blockquote>
  189. <p>
  190. <hr size=4>
  191. <p>
  192. </body>
  193. </html>
  194.